package com.dcbeida.widget.scanitem;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.dcbeida.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class ScanView<T> extends LinearLayout implements IscanView<T> {


    private Map<String, List<T>> listMap;
    private Handler handler = new Handler(Looper.getMainLooper());

    public ScanView(Context context) {
        this(context, null);
    }

    private EmptyListener listener;

    public EmptyListener getListener() {
        return listener;
    }

    public void setListener(EmptyListener listener) {
        this.listener = listener;
    }

    public ScanView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }


    private void initData() {
        listMap = new HashMap<>();

    }

    //计算所有数量
    protected abstract void calculateCount(View headView, List<T> list);

    protected abstract View getNoHeadView();

    protected abstract View getHasHeadView();

    protected abstract void setDataToView(View itemView, T data);

    @Override
    public void addData(T data) {
        if (data == null)
            throw new IllegalArgumentException("传入的data不能为空！！！");
        String key = getKey(data);
        View itemView;
        List<T> list;
        View headView;
        if (listMap.containsKey(key)) {
            list = listMap.get(key);
            list.add(data);
            itemView = getNoHeadView();
            itemView.setTag(key);
            this.addView(itemView, childToIndex(key));
            headView = this.getChildAt(findToHeadIndex(key));
        } else {
            list = new ArrayList<>();
            list.add(data);
            listMap.put(key, list);
            itemView = getHasHeadView();
            itemView.setTag(key);
            this.addView(itemView, childToIndex(key));
            headView = itemView;
        }
        setDataToView(itemView, data);
        calculateCount(headView, list);
    }

    @Override
    public void removeData(T data) {
        if (data == null || !listMap.containsKey(getKey(data)))
            throw new IllegalArgumentException("传入的data不能为空,或者传入data根本不就在map里面自查！！！");
        List<T> list = listMap.get(getKey(data));
        list.remove(data);
        if (!list.isEmpty()) {
            int toHeadIndex = findToHeadIndex(getKey(data));
            View itemWithHeadView = this.getChildAt(toHeadIndex);
            itemWithHeadView.findViewById(R.id.lin_header).setVisibility(VISIBLE);
            calculateCount(itemWithHeadView, list);
        } else {
            listMap.remove(getKey(data));
        }

        if (listMap.isEmpty()) {
            if (listener != null)
                listener.emptyCallBack();
        }
    }

    @Override
    public Map<String, List<T>> getDatas() {
        return listMap;
    }

    @Override
    public int childToIndex(String key) {
        int index = 0;
        boolean isHead = false;
        int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View itemView = this.getChildAt(i);
            if (key.equalsIgnoreCase((String) itemView.getTag())) {
                isHead = true;
                index = i;
            }
        }
        if (isHead) index += 1;
        else index = childCount;
        return index;
    }

    @Override
    public int findToHeadIndex(String key) {
        int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View itemView = this.getChildAt(i);
            if (key.equals(itemView.getTag()))
                return i;
        }
        return childCount;
    }

    public interface EmptyListener {
        void emptyCallBack();
    }
}
